package blog.controller;
import blog.entity.Comment;
import blog.service.DatabaseService;


import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.Cell;
import jxl.JXLException;
import jxl.Sheet;
import jxl.Workbook;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(value="master/*")
public class CommentController {
@Autowired DatabaseService databaseService;
	
	//list Comment
		@RequestMapping(value="comment/searchComment")
		public String listComment(ModelMap modelMap){
			List<Comment> com = databaseService.listComment();
			modelMap.put("comment",com.toArray());
			return "/master/comment/searchComment";
		}
		
		//Find by ID 
		@RequestMapping(value="comment/detailComment", method=RequestMethod.GET)
		public String openComment(@ModelAttribute Comment com, ModelMap modelMap){
			
			com = databaseService.findCommentById(com.getIdc());
			modelMap.put("comment", com);
			return "/master/comment/detailComment";
		}
		
		//update comment
		@RequestMapping(value="comment/detailComment", method=RequestMethod.POST)
		public String updateComment(@ModelAttribute Comment com){
			
			databaseService.saveComment(com);
			return "redirect:searchComment";
		}
		
		//insert comment
		@RequestMapping(value="comment/insertComment")
		public String insertcomment(){
			return "/master/comment/insertComment";
		}
		
		@RequestMapping(value="comment/insertComment", method = RequestMethod.POST)
		public String saveComment(@ModelAttribute Comment com){
			databaseService.saveComment(com);
			return "redirect:searchComment";
		}
		
		//delete
		@RequestMapping(value="comment/deleteComment")
		public String deleteComment(Comment com){
			databaseService.deleteComment(com);
			return "redirect:searchComment";
		}
		
		@RequestMapping(value="comment/upload")
		public String uploadExcel(@ModelAttribute Comment com, 
				HttpServletRequest req, HttpServletResponse res)
				throws IOException, ServletException, JXLException {
			
			ServletInputStream is = req.getInputStream();
			byte[] junk = new byte[1024];
			
			is.readLine(junk, 0, junk.length);
			is.readLine(junk, 0, junk.length);
			is.readLine(junk, 0, junk.length);
			is.readLine(junk, 0, junk.length);
			
			Workbook workbook = Workbook.getWorkbook(is);
			
			Sheet s = workbook.getSheet(0);
			int row = s.getRows();
			int col = s.getColumns();
			Cell data = null;
			
			for(int i = 1; i < row; i++){
				for(int j = 1; j < col; j++){
					data = s.getCell(j, i);
					com.setDate(data.getContents());
					com.setSender(data.getContents());
					com.setComment(data.getContents());
					com.setIdc((Integer) null);
					databaseService.saveComment(com);
				}
			}
			return "redirect:searchComment";
		}
}
